using System;
using System.Collections.Generic;
using System.IdentityModel.Tokens.Jwt;
using System.Linq;
using System.Net;
using System.Security.Claims;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNet.OData;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.IdentityModel.Tokens;
using  server.status_db;
using EFCore.BulkExtensions;
using System.Linq.Dynamic.Core;
using System.Dynamic;
[Authorize]
public class XtzsController : ODataController
{
    private status_oneContext _db;
    public XtzsController(status_oneContext context)
    {
        _db = context;

    }
    private bool XtzsExists(Guid key)
    {
        return _db.PG_B_XTZ.Any(p => p.ID == key);
    }
    [HttpPost]
    public IActionResult BulkAddXtzs(ODataActionParameters parameters)
    {
         if (!ModelState.IsValid)
        {
            var message = string.Join(" | ", ModelState.Values
        .SelectMany(v => v.Errors)
        .Select(e => e.ErrorMessage));
    return   StatusCode(500, message);
        }
        var Sjjyqk = (IEnumerable<PG_B_XTZ>)parameters["Xtzs"];
        //
    
         _db.BulkInsert<PG_B_XTZ>( new List<PG_B_XTZ>(Sjjyqk));
        // Your data access layer logic goes here.
        return this.Ok(HttpStatusCode.NoContent);
    }
    [HttpPut]
    public async Task<IActionResult> Put([FromODataUri] Guid key, [FromBody]PG_B_XTZ update)
    {
        if (!ModelState.IsValid)
        {
            return BadRequest(ModelState);
        }
        if (key != update.ID)
        {
            return BadRequest();
        }
        _db.Entry(update).State = EntityState.Modified;
        try
        {
            await _db.SaveChangesAsync();
        }
        catch (DbUpdateConcurrencyException)
        {
            if (!XtzsExists(key))
            {
                return NotFound();
            }
            else
            {
                throw;
            }
        }
        return Updated(update);
    }
    public async Task<IActionResult> Delete([FromODataUri] Guid key)
    {
        var xnjd = await _db.PG_B_XTZ.FindAsync(key);
        if (xnjd == null)
        {
            return NotFound();
        }
        _db.PG_B_XTZ.Remove(xnjd);
        await _db.SaveChangesAsync();
        return NoContent();
    }
    [HttpPost]
    public async Task<IActionResult> Post([FromBody] PG_B_XTZ model)
    {
      
        if (!ModelState.IsValid)
        {
            
          var message = string.Join(" | ", ModelState.Values
                                            .SelectMany(v => v.Errors)
                                            .Select(e => e.Exception.Message));
    return   StatusCode(500, message);
        }
        _db.PG_B_XTZ.Add(model);
        await _db.SaveChangesAsync();

        return Created(model);
    }
    [EnableQuery]
    public IActionResult Get()
    {
        return Ok(_db.PG_B_XTZ);
    }
    
    //
     [HttpGet("/api/Xtzs/AuditAllData")]
    public IActionResult AuditAllData(String GH,String XM,String status,String XXID, String CJPC)
    {
        
        _db.Database.ExecuteSqlCommand($"update PG_B_XTZ set 审核卡号={GH},审核姓名={XM},审核状态={status},审核时间= GETDATE() where XXID={XXID} and CJPC={CJPC}");
         return NoContent();
    }
    [HttpGet("/api/Xtzs/RemoveAllData")]
   public IActionResult RemoveAllData(String GH,String XM,String XXID, String CJPC){
         _db.Database.ExecuteSqlCommand($"update PG_B_XTZ set 删除卡号={GH},删除姓名={XM},是否删除=1,删除时间= GETDATE() where XXID={XXID}  and  CJPC={CJPC}");
         return NoContent();
    }
[HttpGet("/api/Xtzs/GetData")]
    public IActionResult GetData(String XXID, String CJPC,String query,string order,int limit,int skip,String viewMode)
    {
        //
        Console.WriteLine("query "+query);
        //
        
         var datas = _db.PG_B_XTZ.Where("XXID == @0 and CJPC == @1 and (是否删除 ==0 or 是否删除 == null)",XXID,CJPC);
         if(viewMode == "是")
         datas = datas.Where("审核状态 ==\"通过\"");
         if(query != null)
         datas = datas.Where(query);
         var Sums = new String[35];

        Sums[0]="合计";
         Sums[5]="系部数:"+datas.Select(e=>e.BMBH).Distinct().Count();
         Sums[7]="专业数:"+datas.Select(e=>e.ZYDM).Distinct().Count();
         Sums[9]="方向数:"+datas.Select(e=>e.ZYFXDM).Distinct().Count();
         Sums[11]=":"+datas.Select(e=>e.BJS).Sum();
         Sums[12]=""+datas.Select(e=>e.PYXTS).Sum();
         Sums[13]=""+datas.Select(e=>e.QYSFS).Sum();
         Sums[14]=""+datas.Select(e=>e.QYJXKS).Sum();
         Sums[15]=""+datas.Select(e=>e.XXNTRJF).Sum();
         Sums[16]=""+datas.Select(e=>e.QYNTRJF).Sum();
         Sums[4]= "通过:"+datas.Where(e=>e.审核状态 == "通过").Count();
         Sums[4]+= ",不通过:"+datas.Where(e=>e.审核状态 == "不通过").Count();
         Sums[4] += ",待审核:"+datas.Where(e=>e.审核状态 == "待审核").Count();
          
        dynamic temp = new  ExpandoObject();
        temp.sums = Sums;
        temp.total = datas.Count();
        temp.values = datas.OrderBy(order).Skip(skip).Take(limit).ToArray();
        
        return this.Ok(temp);
    }
    // [HttpGet("/Xtzs/GetSummary")]
    // public IActionResult GetSummary(String XXID,String CJPC)
    // {
        
    //     var Sums = new String[35];

    //     Sums[0]="合计";
    //      Sums[5]="系部数:"+_db.PG_B_XTZ.Select(e=>e.BMBH).Distinct().Count();
    //      Sums[7]="专业数:"+_db.PG_B_XTZ.Select(e=>e.ZYDM).Distinct().Count();
    //      Sums[9]="方向数:"+_db.PG_B_XTZ.Select(e=>e.ZYFXDM).Distinct().Count();
    //      Sums[11]=":"+_db.PG_B_XTZ.Select(e=>e.BJS).Sum();
    //      Sums[12]=""+_db.PG_B_XTZ.Select(e=>e.PYXTS).Sum();
    //      Sums[13]=""+_db.PG_B_XTZ.Select(e=>e.QYSFS).Sum();
    //      Sums[14]=""+_db.PG_B_XTZ.Select(e=>e.QYJXKS).Sum();
    //      Sums[15]=""+_db.PG_B_XTZ.Select(e=>e.XXNTRJF).Sum();
    //      Sums[16]=""+_db.PG_B_XTZ.Select(e=>e.QYNTRJF).Sum();
    //      Sums[33]= "通过:"+_db.PG_B_XTZ.Where(e=>e.审核状态 == "通过").Count();
    //      Sums[33]+= ",不通过:"+_db.PG_B_XTZ.Where(e=>e.审核状态 == "不通过").Count();
    //      Sums[33] += ",待审核:"+_db.PG_B_XTZ.Where(e=>e.审核状态 == "待审核").Count();

    //     return this.Ok(Sums);
    // }
}